<template>
  <el-dialog
    :title="formData.id ? '编辑配置' : '新增配置'"
    :visible="dialogVisible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    center
  >
    <el-form
      ref="addEditForm"
      v-loading="isLoading > 0"
      :model="formData"
      :rules="rules"
      label-width="100px"
      class="add-edit-form"
    >
      <el-form-item label="用户ID">
        <span>{{ formData.user_id }}</span>
      </el-form-item>

      <el-form-item label="优惠券类型" prop="limit_min_money">
        <el-select v-model="formData.coupon_cate_id" placeholder="选择分类">
          <el-option
            v-for="item in couponCateList"
            :key="item.id"
            :label="item.coupon_name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item class="submit-box">
        <el-button type="primary" @click="submitForm()">提交</el-button>
        <el-button v-if="!formData.id" @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
// import _ from 'lodash'
// const formData = {
//   coupon_name: '',
//   limit_min_money: null,
//   money: null,
//   term: null,
//   start_time: new Date(),
//   end_time: new Date(),
//   status: -1
// }
import { getCouponCateList, addOneCouponUser, updateOneCouponUserById } from '@/api/coupon'
export default {
  name: 'CouponAddEdit',
  props: {},
  data() {
    return {
      dialogVisible: false,
      isLoading: 0,
      couponCateList: [],
      formData: {},
      rules: {
        coupon_name: [
          { required: true, message: '不能为空', trigger: ['change', 'blur'] }
        ],
        longitude: [
          { required: true, message: '不能为空', trigger: ['change', 'blur'] },
          { pattern: /^[1-9]\d*\.?\d*$/, message: '必须是一个数字', trigger: ['change', 'blur'] }
        ],
        latitude: [
          { required: true, message: '不能为空', trigger: ['change', 'blur'] },
          { pattern: /^[1-9]\d*\.?\d*$/, message: '必须是一个数字', trigger: ['change', 'blur'] }
        ],
        map_scale: [
          { required: true, message: '不能为空', trigger: ['change', 'blur'] },
          { pattern: /^\d+$/, message: '必须是一个整数', trigger: ['change', 'blur'] }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getCouponCateList()
  },
  mounted() {},
  methods: {
    async getCouponCateList() {
      try {
        this.isLoading++
        this.couponCateList = await getCouponCateList({ status: 1 })
      } finally {
        this.isLoading--
      }
    },
    handleClose() {
      this.dialogVisible = false
    },
    open() {
      this.dialogVisible = true
      this.formData = {
        user_id: Number.parseInt(this.$route.query.user_id),
        coupon_cate_id: null
      }
      this.$nextTick(() => {
        this.$refs.addEditForm.clearValidate()
      })
    },
    resetForm() {
      this.formData = {
        user_id: Number.parseInt(this.$route.query.user_id),
        coupon_cate_id: null
      }
      this.$refs.addEditForm.resetFields()
    },
    submitForm() {
      if (this.isLoading > 0) return
      this.$refs.addEditForm.validate(async vaild => {
        if (!vaild) return
        try {
          this.isLoading++

          if (this.formData.id) {
            await updateOneCouponUserById(this.formData)
          } else {
            await addOneCouponUser(this.formData)
          }
          this.$emit('refresh')
          this.handleClose()
        } finally {
          this.isLoading--
        }
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.submit-box {
  display: flex;
  justify-content: flex-end;
}

.new-goods-box,
.activity-goods-box,
.recharge-box {
  li {
    display: flex;
    align-items: center;
  }
  .input {
    margin: 20px;
  }
  .icon {
    cursor: pointer;
    font-size: 30px;
    color: #409eff;
  }
}
</style>
